package strings

import (
	"encoding/base64"
	"encoding/json"
	"regexp"
	"strings"
	"unicode"
)

func Is(s string, pattern string) bool {
	match, _ := regexp.MatchString(pattern, s)
	return match
}

func IsJSON(s string) bool {
	var js json.RawMessage
	return json.Unmarshal([]byte(s), &js) == nil
}

func IsJSONArray(s string) bool {
	var arr []interface{}
	err := json.Unmarshal([]byte(s), &arr)
	return err == nil
}

func IsNumeric(s string) bool {
	for _, char := range s {
		if !unicode.IsDigit(char) {
			return false
		}
	}
	return true
}

func IsHex(s string) bool {
	hexRegex := regexp.MustCompile(`^[0-9a-fA-F]+$`)
	return hexRegex.MatchString(s)
}

func IsBase64(s string) bool {
	_, err := base64.StdEncoding.DecodeString(s)
	return err == nil
}

func Contains(s, substr string) bool {
	return strings.Contains(s, substr)
}

func StartsWith(s string, prefixes []string) bool {
	for _, prefix := range prefixes {
		if strings.HasPrefix(s, prefix) {
			return true
		}
	}
	return false
}

func EndsWith(s string, suffixes []string) bool {
	for _, suffix := range suffixes {
		if strings.HasSuffix(s, suffix) {
			return true
		}
	}
	return false
}

func ContainsAll(s string, subs []string) bool {
	for _, sub := range subs {
		if !strings.Contains(s, sub) {
			return false
		}
	}
	return true
}
